package com.afishing.servlet;

import com.afishing.entity.User;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;

@WebServlet(name = "ChatActionServlet", value = "/chatAction")
public class ChatActionServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        ServletContext application = this.getServletContext();

        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        HttpSession session = request.getSession();

        User user = (User) session.getAttribute("user");

        String msg = request.getParameter("msg");
        ArrayList<String> msgs = (ArrayList) application.getAttribute("msgs");
        if (msgs == null) msgs = new ArrayList();
        msgs.add(user.getUsername() + "说：" + msg);
//        application.setAttribute("msgs", msgs);
        response.sendRedirect("chatroom.jsp");

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
